"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 二级分区
Case Name   : range_hash二级分区表列约束:primary key/约束推迟/using index tablespace
Create At   : 2022/2/24
Owner       : @daiguatutu
Description :
    1.创建二级分区表,二级分区键包含列约束主键
    2.查看索引类型
    3.创建二级分区表,一级分区键包含列约束主键
    4.查看索引类型
    5.插入数据
    6.插入重复数据
    7.创建二级分区表，一级分区键包含列约束主键
    8.开启事务并设置约束在事务提交时检查,插入重复数据后commit
    9.创建二级分区表，一级分区键包含列约束主键，using index表空间
    10.列出所有名称以t_subpartition_0315开头的表、视图和索引
    11.创建二级分区表，普通列包含列约束主键，using index表空间
    12.列出所有名称以t_subpartition_0315开头的表、视图和索引
    13.清理环境
Expect      :
    1.成功
    2.成功,global类型
    3.成功
    4.成功,global类型
    5.成功
    6.合理报错
    7.成功
    8.插入重复数据成功,commit合理报错
    9.成功
    10.成功,索引表空间为ts_subpartition_0315
    11.成功
    12.成功,索引表空间为ts_subpartition_0315
    13.清理成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Subpartition(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_subpartition_0315'
        self.ts_name = 'ts_subpartition_0315'

    def test_subpartition(self):
        text = '-----step1:创建二级分区表,二级分区键包含列约束主键 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int ,col_2 int primary key,col_3 int ,col_4 int) " \
            f"partition by range (col_1) subpartition by hash (col_2)" \
            f"( partition p_range_1 values less than( -10 )" \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3)," \
            f"  partition p_range_2 values less than( 20 )," \
            f"  partition p_range_3 values less than( 30)" \
            f"  ( subpartition p_hash_3_1 , subpartition p_hash_3_2 ," \
            f"    subpartition p_hash_3_3)," \
            f"    partition p_range_4 values less than( 50)" \
            f"  ( subpartition p_hash_4_1 , subpartition p_hash_4_2 ," \
            f"    subpartition t_subpartition_0350)," \
            f"  partition p_range_5 values less than( maxvalue )" \
            f") enable row movement;"
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step2:查看索引类型 expect:成功,global类型-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\di')
        self.log.info(msg)
        self.assertIn('global partition index', msg, '执行失败:' + text)

        text = '-----step3:创建二级分区表,一级分区键包含列约束主键 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int primary key ,col_2 int ,col_3 int ,col_4 int) " \
            f"partition by range (col_1) subpartition by hash (col_2)" \
            f"( partition p_range_1 values less than( -10 )" \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3)," \
            f"  partition p_range_2 values less than( 20 )," \
            f"  partition p_range_3 values less than( 30)" \
            f"  ( subpartition p_hash_3_1 , subpartition p_hash_3_2 ," \
            f"    subpartition p_hash_3_3)," \
            f"    partition p_range_4 values less than( 50)" \
            f"  ( subpartition p_hash_4_1 , subpartition p_hash_4_2 ," \
            f"    subpartition t_subpartition_0350)," \
            f"  partition p_range_5 values less than( maxvalue )" \
            f") enable row movement;"
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step4:查看索引类型 expect:成功,global类型-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\di')
        self.log.info(msg)
        self.assertIn('global partition index', msg, '执行失败:' + text)

        text = '-----step5:插入数据 expect:成功-----'
        self.log.info(text)
        insert_sql = f"insert into {self.t_name} values(1,1,1,1);"
        msg = self.pri_sh.execut_db_sql(insert_sql)
        self.log.info(msg)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step6:插入重复数据 expect:合理报错-----'
        self.log.info(text)
        insert_sql = f"insert into {self.t_name} values(1,1,1,1);"
        msg = self.pri_sh.execut_db_sql(insert_sql)
        self.log.info(msg)
        self.assertIn(self.constant.unique_index_error_info, msg,
                      '执行失败:' + text)

        text = '-----step7:创建二级分区表，一级分区键包含列约束主键 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int primary key  deferrable ,col_2 int ," \
            f"col_3 int ,col_4 int) " \
            f"partition by range (col_1) subpartition by hash (col_2)" \
            f"( partition p_range_1 values less than( -10 )" \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3)," \
            f"  partition p_range_2 values less than( 20 )," \
            f"  partition p_range_3 values less than( 30)" \
            f"  ( subpartition p_hash_3_1 , subpartition p_hash_3_2 ," \
            f"    subpartition p_hash_3_3)," \
            f"    partition p_range_4 values less than( 50)" \
            f"  ( subpartition p_hash_4_1 , subpartition p_hash_4_2 ," \
            f"    subpartition t_subpartition_0350)," \
            f"  partition p_range_5 values less than( maxvalue )" \
            f") enable row movement;"
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step8:开启事务并设置约束在事务提交时检查,插入重复数据后commit ' \
               'expect:插入重复数据成功,commit合理报错-----'
        self.log.info(text)
        sql = "start transaction;" \
            f"set constraints all deferred;" \
            f"insert into {self.t_name} values(111,111),(205,205);" \
            f"insert into {self.t_name} values(111,111),(205,205);" \
            f"commit;"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.constant.START_TRANSACTION_SUCCESS_MSG, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.SET_SUCCESS_MSG, msg, '执行失败:' + text)
        self.assertEqual(2, msg.count(self.constant.INSERT_SUCCESS_MSG),
                         '执行失败:' + text)
        self.assertIn(self.constant.unique_index_error_info, msg,
                      '执行失败:' + text)

        text = '-----step9:创建二级分区表，一级分区键包含列约束主键，using index表空间 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"drop tablespace if exists {self.ts_name};" \
            f"create tablespace {self.ts_name} relative location " \
            f"'{self.ts_name}';" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int primary key using index tablespace {self.ts_name} ," \
            f"col_2 int ,col_3 int ,col_4 int) " \
            f"partition by range (col_1) subpartition by hash (col_2)" \
            f"( partition p_range_1 values less than( -10 )" \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3)," \
            f"  partition p_range_2 values less than( 20 )," \
            f"  partition p_range_3 values less than( 30)" \
            f"  ( subpartition p_hash_3_1 , subpartition p_hash_3_2 ," \
            f"    subpartition p_hash_3_3)," \
            f"    partition p_range_4 values less than( 50)" \
            f"  ( subpartition p_hash_4_1 , subpartition p_hash_4_2 ," \
            f"    subpartition t_subpartition_0350)," \
            f"  partition p_range_5 values less than( maxvalue )" \
            f") enable row movement;"
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step10:列出所有名称以t_subpartition_0315开头的表、视图和索引 ' \
               'expect:成功,索引表空间为ts_subpartition_0315-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\d+ t_subpartition_0315')
        self.log.info(msg)
        self.assertIn(f'btree (col_1) TABLESPACE {self.ts_name}', msg,
                      '执行失败:' + text)

        text = '-----step11:创建二级分区表，普通列包含列约束主键，using index表空间 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"drop tablespace if exists {self.ts_name};" \
            f"create tablespace {self.ts_name} relative location " \
            f"'{self.ts_name}';" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int ,col_2 int ,col_3 int ," \
            f"col_4 int primary key using index tablespace {self.ts_name}) " \
            f"partition by range (col_1) subpartition by hash (col_2)" \
            f"( partition p_range_1 values less than( -10 )" \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3)," \
            f"  partition p_range_2 values less than( 20 )," \
            f"  partition p_range_3 values less than( 30)" \
            f"  ( subpartition p_hash_3_1 , subpartition p_hash_3_2 ," \
            f"    subpartition p_hash_3_3)," \
            f"    partition p_range_4 values less than( 50)" \
            f"  ( subpartition p_hash_4_1 , subpartition p_hash_4_2 ," \
            f"    subpartition t_subpartition_0350)," \
            f"  partition p_range_5 values less than( maxvalue )" \
            f") enable row movement;"
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step12:列出所有名称以t_subpartition_0315开头的表、视图和索引 ' \
               'expect:成功,索引表空间为ts_subpartition_0315-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\d+ t_subpartition_0315')
        self.log.info(msg)
        self.assertIn(f'btree (col_4) TABLESPACE {self.ts_name}', msg,
                      '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----step13:清理环境-----')
        text_1 = '-----删除表和表空间 expect:成功-----'
        self.log.info(text_1)
        del_cmd = f'drop table if exists {self.t_name};' \
            f'drop tablespace if exists {self.ts_name};'
        msg = self.pri_sh.execut_db_sql(del_cmd)
        self.log.info(msg)

        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, msg,
                      '执行失败:' + text_1)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg, '执行失败:' + text_1)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
